import { PGCServer } from "../../PGCServer";
import { IListDataRefreshHandle } from "../../tool/list/IListDataRefreshHandle";
import { DressUpData } from "./DressUpData";

/**
 * 角色模板数据
 */

export class DressUpTempMgr implements IListDataRefreshHandle {
    /**分页数量*/
    pageSize: number = 20;
    /**分页*/
    page: number = 0;
    /**数据*/
    datas: any[] = [];

    private _maxPage: number = -1;


    isLastList: boolean;

    startPage: number = 0; // 起始页数

    static requestNum:number;

    public isRequest:boolean; //  是否有请求数据;

    constructor() {
    }

    refreshData(callback?: Laya.Handler) {
        this.clear();
        this.getNextPageData(callback);
    }

    getNextPageData(callback: Laya.Handler) {
        let datas = this.datas;

        if (datas.length >= this.pageSize * (this.page + 1))
            this.page += 1;

        
        // if (this._maxPage < this.page) {
        let realPageSize = this.pageSize;

        PGCServer.ins.getDeveloperAvatar(this.page, this.pageSize, Laya.Handler.create(this, (data: any) => {
            if (!data.ret) {
                this.isRequest = true;
                let list: DressUpData[] = data.data;
                list.forEach((item:any)=>{ // 转换数据方便使用
                    item.preform_id = item.developer_avatar_id;
                    item.iconUrl = item.developer_avatar_img;
                    item.preform_name = item.developer_avatar_name;
                });

                
                if (list.length > 0 && this._maxPage < this.page) {
                    this._maxPage = Math.max(this.page, this._maxPage);
                    this.datas = datas.concat(list);
                    DressUpTempMgr.requestNum = this.datas.length;
                }
                // else if(list.length > 0){
                //     if(list.length > this.datas.length){
                //         this.datas = list;
                //     }else if(list.length < this.datas.length){
                //         this.datas = datas.concat(list);
                //     }
                //     console.log("达到极限了-------------");
                // }
            }
            callback != null && callback.runWith([this.datas]);//直接返回数据

        }), Laya.Handler.create(this, (data: any) => {
            callback != null && callback.runWith([this.datas]);//直接返回数据
            console.error("data onClickBoard ", data);
        }));
        // }
    }

    uploadTemp(id: number, name: string, iconUrl: string, avatarJson: string, callback: Laya.Handler){
        PGCServer.ins.updateDeveloperAvatar(id, name, iconUrl, avatarJson, callback);
    }

    /** 是否存在模板数据 */
    public existTemp(callback:Laya.Handler): void{
        if(this.isRequest){
            callback && callback.runWith(DressUpTempMgr.requestNum > 0);
        }else{

            this.refreshData(Laya.Handler.create(this, this.existTemp, [callback]));
        }
    }

    public clear() {
        this.page = 0;
        this.datas.length = 0;
        this._maxPage = -1;
        this.isRequest = false;
    }
}

